package day320;

import java.util.Stack;

/**
 * @author tmh
 * @date 2023/3/20 21:43
 * @description 最小栈
 * 拓展：用栈来实现队列，用队列实现栈
 */
public class T155 {
}

class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public MinStack() {
        stack=new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if(minStack.isEmpty()||val<=minStack.peek()){
            minStack.push(val);
        }
    }

    public void pop() {
        if(stack.peek().equals(minStack.peek())){
            stack.pop();
            minStack.pop();
        }else {
            stack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}
